package com.gbiac.modules.monitor.listener;

import com.gbiac.modules.monitor.event.AiDeviceCloseCameraTaskEvent;
import com.gbiac.modules.monitor.event.AiDeviceOpenCameraTaskEvent;
import com.gbiac.modules.monitor.service.AiDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 全局监听器
 */
@Slf4j
@Component
public class GlobarListener {


    @Resource
    private AiDeviceService aiDeviceService;

    @Async
    @EventListener
    public void handlerOpenCameraTaskEvent(AiDeviceOpenCameraTaskEvent event) {
        Integer num = event.getNum();
        for (int i = 0; i < num ; i++) {
            Boolean b = aiDeviceService.openCameraTask(event.getAiDeviceCameraRelationDTO());
            if(b != null && b){
                break;
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Async
    @EventListener
    public void handlerCloseCameraTaskEvent(AiDeviceCloseCameraTaskEvent event) {
        Integer num = event.getNum();
        for (int i = 0; i < num ; i++) {
            Boolean b = aiDeviceService.closeCameraTask(event.getCameraCode());
            if(b != null && b){
                break;
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
